package com.hopu.v1.controller.admin.verify;

import com.hopu.v1.pojo.common.PageData;
import com.hopu.v1.pojo.common.R;
import com.hopu.v1.pojo.verify.Approval;
import com.hopu.v1.pojo.verify.StudentLeaveApplications;
import com.hopu.v1.service.admin.verify.ApprovalService;
import com.hopu.v1.service.admin.verify.VerifyStudentLeaveApplicationsService;
import com.hopu.v1.utils.JwtUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.List;

/**
 * 学生请假申请表(StudentLeaveApplications)表控制层
 *
 * @author makejava
 * @since 2024-09-30 14:41:54
 */
@CrossOrigin
@RestController
@RequestMapping("/api/v1/admin/verify/studentLeaveApplications")
public class VerifyStudentLeaveApplicationsController {
    /**
     * 服务对象
     */
    @Resource
    private VerifyStudentLeaveApplicationsService studentLeaveApplicationsService;

    @Resource
    private ApprovalService approvalService;


    /**
     * 根据请假记录id和审批人id修改审批状态
     * @param approval 审批对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Approval approval,HttpServletRequest request) {
        Integer staffId = getStaffIdByToken(request);
        approval.setStaffId(staffId);
        approvalService.updateApproval(approval);
        return R.ok("修改成功");
    }

    /**
     * 根据token获取员工id
     * @param request 请求
     * @return 员工id
     */
    public Integer getStaffIdByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        String staffId = JwtUtils.getUserId(token);
        //返回员工id
        return Integer.parseInt(staffId);
    }
    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(HttpServletRequest request,Integer current, Integer size) {
        //根据token获取staffId
        Integer staffId1 = getStaffIdByToken(request);
        PageData<StudentLeaveApplications> page1 = studentLeaveApplicationsService.selectLeaveList(staffId1,current, size);
        List<StudentLeaveApplications> records = page1.getData();
        for (StudentLeaveApplications record : records) {
            Integer id = record.getId();
            //根据请假记录id查询审批表的数据，存放到对象
            List<Approval> approvals = approvalService.selectLeaveRecordId(id);
            // 遍历approvals 集合，如果approval_status为1且长度为2则修改status为1，
            // 如果approval_status有一个0则修改status状态为0，如果approval_status有一个2则修改status状态为2

            // 统计状态为 1 和 2 的记录的数量
            int count1 = 0;
            int count2 = 0;


            for (Approval approval1 : approvals) {
                // 如果当前 Approval 对象的状态为 1，则增加计数
                if (approval1.getApprovalStatus() == 1) {
                    count1++;
                } else if (approval1.getApprovalStatus() == 2) {
                    count2++;
                }
            }
            for (Approval approval : approvals) {
                // 判断状态为 1 的记录数量是否为 2
                if (count1 == 2) {
                    // 设置 record 的状态为 1
                    record.setStatus(1);
                    // 通过服务类更新数据库中的记录
                    studentLeaveApplicationsService.updateById(record);
                }else if (approval.getApprovalStatus() == 2 ) {
                    // 设置 record 的状态为 2
                    record.setStatus(2);
                    // 通过服务类更新数据库中的记录
                    studentLeaveApplicationsService.updateById(record);
                } else if (approval.getApprovalStatus() == 0 && count2 == 0) {
                    // 设置 record 的状态为 0
                    record.setStatus(0);
                    // 通过服务类更新数据库中的记录
                    studentLeaveApplicationsService.updateById(record);
                }
            }
        }
        return R.ok(page1);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.studentLeaveApplicationsService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param studentLeaveApplications 实体对象
     * @return 新增结果
     */
    @PostMapping
    public R insert(@RequestBody StudentLeaveApplications studentLeaveApplications) {
        return R.ok(this.studentLeaveApplicationsService.save(studentLeaveApplications));
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.studentLeaveApplicationsService.removeByIds(idList));
    }
}

